﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.News
{
    public class NewsSearcher : INewsSearcher
    {
        public PagedResult<Article> FindArticles(Guid categoryID, int start, int limit)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                List<Article> articleList = context.Set<Article>().Where(p => p.CategoryId == categoryID)
                     .OrderByDescending(p => p.CreateDate)
                     .Take(limit)
                     .Skip(start)
                     .ToList();
                int total = context.Set<Article>().Where(p => p.CategoryId == categoryID).Count();

                return new PagedResult<Article>(start, limit, total, articleList);

                //string sql = "select a.* from news_article as a where a.title like {0}";
                //filter = string.Format("%{0}%", filter);
                //List<Article> articleList = context.Database.SqlQuery<Article>(sql, filter)
                //    .OrderBy(p => p.Title)
                //    .Take(limit)
                //    .Skip(start)
                //    .ToList();
                //int total = context.Database.SqlQuery<Article>(sql, filter).Count();

                //return new PagedResult<Article>(start, limit, total, articleList);
            }
        }
    }
}
